from calendar import isleap
import datetime
import random

def ChangePassword (user,ori_p,new_p,UD,OM): #修改密码
    if user == '男朋友':
        identity = 0
    else:
        identity = 1

    r = UD.PasswordChange (identity,ori_p,new_p)

    if r == 1:
        OM.SaveObj(UD,'./data/UserData.pkl')
    
    return r


def AnniversaryError (year,month,day,days,tyear,tmonth,tday): #处理输入纪念日的错误
    if not year.isdigit() or not month.isdigit() or not day.isdigit(): #非正整数
        return 1

    if int (year) > tyear or int (year) < 1900: #年份超出范围
        return 1

    if int (year) == tyear and int (month) > tmonth: #月份超出今年范围
        return 1

    if int (year) == tyear and int (month) == tmonth and int (day) > tday: 
    #日期超出今年范围
        return 1

    if isleap (int (year)):
        days[2] = 29
    else:
        days[2] = 28

    if int (month) < 1 or int (month) > 12: #月份超出范围
        return 1

    if int (day) < 1 or int (day) > days[int (month)]: #日期超出范围
        return 1
    
    return 0


def Login (id_text,password,UD,PD,OM): #登录

    if (id_text != '男朋友' and id_text != '女朋友') or password == '':
        return '请将信息填写完整哦！'

    if id_text == '男朋友':
        identity = 0
    else:
        identity = 1

    r = UD.Login (identity,password)

    if r == 1: #登陆成功
        PD.u_identity = identity

        if UD.UserList[PD.u_identity]['Level'] == 10:
            if (UD.last[PD.u_identity] - datetime.date.today()).days >= 30:
                UD.item[PD.u_identity]['Key'] += 1
                UD.last[PD.u_identity] = datetime.date.today()

            OM.SaveObj (UD,'./data/UserData.pkl')

        return ''
        
    elif r == 0: #密码错误
        return '密码填写错误啦！'
        
    else: #身份未注册
        return '该身份还未注册哦！'


def DiaryDecry (UD,PD): #解锁日记功能
    if UD.UserList[PD.u_identity]['Level'] < 2:
        return [True,'./UI/Main/BB3_Disabled.png']
    else:
        return [False,'./UI/Main/BB3.png']


def Level (UD,PD): #等级信息
    LV = UD.UserList[PD.u_identity]['Level']
    EXP = UD.UserList[PD.u_identity]['Exp']

    if LV == 10:
        return ['./UI/Main/LV10.png','./UI/Main/EXPmax.png','当前等级：10级','经验已满']

    else:
        exp_max = UD.level_Exp[LV]
        lexp_max = UD.level_Exp[LV-1]

        exp_rate = int ((EXP - lexp_max) / (exp_max - lexp_max) *10)

        return ['./UI/Main/LV' + str (LV) + '.png',\
        './UI/Main/EXP' + str (exp_rate) + '_' + str (PD.u_identity) + '.png',\
        '当前等级：' + str (LV) +'级','距离升级：' + str (exp_max - EXP) + '经验']


def LEADecry (UD,PD): #本地情绪分析解锁
    if UD.UserList[PD.u_identity]['Level'] < 5:
        return True
    else:
        return False


def Activated (UD): #判断系统是否激活
    if UD.anniversary == None:
        return 'firstuse'
    else:
        return 'login'


def Register (identity_text,year,month,day,password_text,nickname_text,tyear,tmonth,tday,UD,OM): #注册
    days = [0,31,28,31,30,31,30,31,31,30,31,30,31]

    if identity_text == ' 选择身份' or year == '' or month == '' or day == '' or \
    password_text == '' or nickname_text == '': #信息未填写完整
        return '请将信息填写完整哦！'

    #判断日期是否合法 

    if not year.isdigit() or not month.isdigit() or not day.isdigit(): #非正整数
        return '您输入的日期有误哦！'

    if int (year) > tyear or int (year) < 1900: #年份超出范围
        return '您输入的日期有误哦！'

    if int (year) == tyear and int (month) > tmonth: #月份超出今年范围
        return '您输入的日期有误哦！'

    if int (year) == tyear and int (month) == tmonth and int (day) > tday: 
    #日期超出今年范围
        return '您输入的日期有误哦！'

    if isleap (int (year)):
        days[2] = 29
    else:
        days[2] = 28

    if int (month) < 1 or int (month) > 12: #月份超出范围
        return '您输入的日期有误哦！'

    if int (day) < 1 or int (day) > days[int (month)]: #日期超出范围
        return '您输入的日期有误哦！'

    #数据输入
    if identity_text == '男朋友':
        identity = 0
    else:
        identity = 1
        
    birthday = datetime.date(int (year),int (month),int (day))
    password = password_text
    nickname = nickname_text
    r = UD.Register (identity,birthday,password,nickname)

    if r == 1: #注册成功
        OM.SaveObj(UD,'./data/UserData.pkl')
        return ''
        
    else: #身份已经被注册
        return '该身份已被注册了哦！'


def NextFunc (func_num,func): #判断下一个功能
    next_id = (func + 1) % func_num

    f_name = ''

    if next_id == 0:
        f_name = 'FM'
    elif next_id == 1:
        f_name = 'EA'
    elif next_id == 2:
        f_name = 'D'
    elif next_id == 3:
        f_name = 'LV'

    path = './UI/Help/' + f_name +'I1.png'
    return [next_id,path]


def ResetHelp (func,page,mode): #切换显示图片
    f_name = ''
    info_pages = [3,3,3,1]

    if func == 0:
        f_name = 'FM'
    elif func == 1:
        f_name = 'EA'
    elif func == 2:
        f_name = 'D'
    elif func == 3:
        f_name = 'LV'

    if mode == 0:
        path = './UI/Help/' + f_name + 'I' + str ((page + 1) % info_pages[func] + 1) + '.png'

    elif mode == 1:
        if page == 0:
            path = './UI/Help/' + f_name + 'I' + str (info_pages[func]) + '.png'
        else:    
            path = './UI/Help/' + f_name + 'I' + str (page) + '.png'


    return path
